package com.ys.vo;

import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
	
    public enum Code {
    	
    	SUCCESS("成功", 0), FAILED("失败", 1);
    	public String name;
    	public Integer value;

    	private Code(String name, Integer value) {
    		this.name = name;
    		this.value = value;
    	}
    }

    public R() {
        put("code", 0);
        put("msg", "操作成功");
    }
    
    public Integer getCode() {
    	return Integer.parseInt(this.get("code").toString());
    }
    
    public String getMsg() {
    	return this.get("msg").toString();
    }
    
    public JSONObject getJO() {
    	return JSON.parseObject(this.get("data").toString());
    }

    public static R error() {
        return error(1, "操作失败");
    }

    public static R error(String msg) {
        return error(1, msg);
    }
    
    public static R errorLogin(String msg) {
        return error(402, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Object object) {
        R r = new R();
        r.put("data", object);
        return r;
    }

    public static R ok(String msg, Object object) {
        R r = new R();
        r.put("msg", msg);
        r.put("data", object);
        return r;
    }
    
    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
